﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_ucThongtincanhanphuhuynh : System.Web.UI.UserControl
{
    BUS_Children objC = new BUS_Children();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ChildrenLogin"] != null)
        {
            Hienthi();
        }
            
    }
    public void Hienthi()
    {
        Page.Title = "Xem thông tin cá nhân";
        DateTime ngay;
        int id = int.Parse(Session["id"].ToString());
        var dataC = objC.BUS_Children_getDataById(id);
        txtid.Text = dataC.Rows[0]["id"].ToString().Trim();
        fullname.Text = dataC.Rows[0]["fullname"].ToString().Trim();
        username.Text = dataC.Rows[0]["childrenid"].ToString().Trim();
        address.Text = dataC.Rows[0]["address"].ToString().Trim();
        ngay = Convert.ToDateTime(dataC.Rows[0]["birthday"].ToString().Trim());
        birthday.Text = ngay.ToString("dd/MM/yyyy");
        phone.Text = dataC.Rows[0]["phone"].ToString().Trim();
        string gioitinh = dataC.Rows[0]["gentle"].ToString().Trim();
        if (gioitinh == "Nam") dropgentle.SelectedIndex = 1; else dropgentle.SelectedIndex = 0;
        string hinh = dataC.Rows[0]["photo"].ToString().Trim();
        if (hinh == "")
        {
            photo.ImageUrl = "../Uploads/Children/no.png";
        }else photo.ImageUrl = "../Uploads/Children/" + hinh;
        //dataC.Rows[0]["Name"].ToString();
    }

    //Phuong thuc upload anh
    public bool UploadImage(string imagename)
    {
        bool complete = false;
        //Lay ve ten file anh
        string fname = uploadphoto.FileName;
        //Tach ten file
        string[] tokens = fname.Split(new char[] { '.' });
        //Luu tru dung luong file tinh bang byte
        int bytes = uploadphoto.FileBytes.Length;
        //Kiem tra dung luong file anh
        if (bytes > (1024 * 1024))
        {
            lberr.Text = "Dung lượng ảnh phải nhỏ hơn 4KB";
            return false;
        }
        //Kiem tra dang file upload
        if (tokens[tokens.Length - 1] == "jpg" || tokens[tokens.Length - 1] == "jpeg" || tokens[tokens.Length - 1] == "png" || tokens[tokens.Length - 1] == "gif")
        {
            //Khai bao bien luu tru duong dan den thu muc images
            //Response.Write("<script>alert(\""+Server+"\")</script>");
            string relPath = Server.MapPath("~\\");
            //Khai bao bien luu tru duong dan tuyet doi
            string absolutePath = relPath + "\\Uploads\\Children\\" + imagename;
            //Luu len server
            uploadphoto.SaveAs(absolutePath);
            complete = true;
        }
        else
        {
            lberr.Text = "Đây không phải file ảnh.";
            return false;
        }
        return complete;
    }
    protected void save_Click(object sender, EventArgs e)
    {
            DTO_Children dtoC = new DTO_Children();
            dtoC.ID = int.Parse(txtid.Text.ToString().Trim());
            dtoC.Fullname = fullname.Text.Trim();
            dtoC.Gentle = dropgentle.SelectedValue.ToString();
            //Response.Write("<script>alert(\"" + dtoC.ID + "," + dtoC.Fullname + "," + dtoC.Gentle + "\")</script>");
            dtoC.Adress = address.Text.Trim();
            DateTime strngay = DateTime.Parse(birthday.Text.Trim());
            dtoC.Birthday = strngay.ToString("MM/dd/yyyy");
            dtoC.Phone = phone.Text.Trim();
            dtoC.Status = 1;
            int id = int.Parse(txtid.Text.ToString().Trim());
            var dataC = objC.BUS_Children_getDataById(id);
            string hinh = dataC.Rows[0]["photo"].ToString().Trim();
            //Response.Write(Server.MapPath("~\\"));
            if (uploadphoto.FileName != string.Empty)
            {
                if (hinh == "")
                {
                    Session["isChangePicchil"] = true;
                    dtoC.Photo = id + uploadphoto.FileName.Substring(uploadphoto.FileName.LastIndexOf(@"\") + 1);
                }
                else dtoC.Photo = hinh;
                if (!UploadImage(dtoC.Photo))
                    return;
            }
            else Session["isChangePicchil"] = false;
            if (!(bool)Session["isChangePicchil"])
            {
                dtoC.Photo = hinh;
            }
            if (objC.Children_Update(dtoC))
            {
                Response.Write("<script>alert(\"Cập nhật thông tin thành công <..*!>\")</script>");
                Response.Redirect("Thongtincanhan.aspx");
            }
            else Response.Write("<script>alert(\"Cập nhật không thành công <..*!>\")</script>");
        
    }
}